
# coding:utf-8

import os, json
from lib import flux, tmxlib, pymunk
from lib.fluxext import singleton
from base import config
from system import phy
from views import new_view_from_dict

@singleton
class MapManager(object):
    views = {}
    layers = {}

    def init(self, scr):
        self.scr = scr
        self.mapcenter = flux.View()

        self.tmx_map = flux.utils.TmxMap()
        self.tmx_map.SetTileSize(flux.utils.Vec2f(config.TILESIZE, config.TILESIZE))

        self.map_top_left = flux.View()
        self.map_top_left.SetAnchor(self.mapcenter)
        self.SetPos(config.SCREEN_WIDTH/2, config.SCREEN_HEIGHT/2)

    def GetPxWidth(self):
        return self.m.width * config.TILESIZE

    def GetPxHeight(self):
        return self.m.height * config.TILESIZE

    def GetWidth(self):
        return self.m.width

    def GetHeight(self):
        return self.m.height

    def SetPos(self, x, y):
        self.mapcenter.SetPos(x, y)

    def load(self, map_name):
        if os.path.exists(map_name):
            f = open(map_name)
            data = f.read()
            f.close()

            data = json.loads(data)
            self.code_file = data['code'].encode('utf-8')
            self.tmx_map_file = data['tmx_map'].encode('utf-8')

            if data['ver'] == 1:
                code = self.code_file
                views = data['views']

                # 清空旧数据
                self.views = {}

                # 准备代码文件
                if code:
                    mod = code.replace('/', '.')[:-3]
                    code = __import__(mod, None, None, mod)

                # 加载前事件
                if code:
                    code.before_views_load(self.views)

                # 加载 views
                for k,v in views.items():
                    k = int(k)
                    for i in v:
                        _ = new_view_from_dict(i)
                        self.views[_.name] = _
                        _.AddToScreen(self.scr, k)

                # 加载后事件
                if code:
                    code.after_views_load(self.views)

                # 开始加载TMX
                for k,v in self.layers.items():
                    self.scr.RemoveView(v)
                self.layers = {}

                self.m = tmxlib.Map.open(self.tmx_map_file)
                self.tmx_map.Load(self.tmx_map_file)

                for layer in self.m.layers:
                    lv = flux.TmxLayerView(self.tmx_map, layer.index)
                    lv.SetAnchor(self.mapcenter)
                    lv.AddToScreen(self.scr, 0)
                    self.layers[layer.index] = lv

                    for i in range(self.m.width):
                        for j in range(self.m.height):
                            if not layer[i, j].gid:
                                continue

                self.map_top_left.SetPos(-self.GetPxWidth()/2, -self.GetPxHeight()/2)

                # 全部加载完成事件
                if code:
                    code.load_done(self.views, self)
